'use strict'

/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findMedianSortedArrays = function(nums1, nums2) {
    const len = nums1.length + nums2.length;
    const all = [];
    let i = 0;
    let j = 0;
    while(true) {
        if (i >= nums1.length) {
            if (j >= nums2.length) {
                break;
            }
            all.push(nums2[j++]);
        } else {
            if (j >= nums2.length || nums1[i] <= nums2[j]) {
                all.push(nums1[i++]);
            } else {
                all.push(nums2[j++]);
            }
        }
    }
    if (len % 2 == 0) {
        return (all[len/2] + all[len / 2 - 1]) / 2.0;
    } else {
        return all[(len-1) / 2];
    }
}

console.log(findMedianSortedArrays([1,3], [2, 4]));
